//
//  Home.swift
//  SwiftuiUno
//
//  Created by baozi on 2023/11/27.
//

import SwiftUI

struct Home: View {
    @Binding var selectedTab: SideItemType
    init(selectedTab: Binding<SideItemType>) {
        self._selectedTab = selectedTab
        UITabBar.appearance().isHidden = true
    }
    
    var body: some View {
        TabView(selection: $selectedTab) {
            HomePage()
                .tag(SideItemType.home)
            AccountAboutPage()
                .tag(SideItemType.security)
            AboutUsPage()
                .tag(SideItemType.about)
            HelpPage()
                .tag(SideItemType.help)
        }
        .tabViewStyle(.automatic)
    }
}

#Preview {
    HomePage()
}


struct HomePage: View  {
    @EnvironmentObject var router: Router
    @State var items: [TicProducter] = []
    var body: some View {
        ZStack {
            VStack {
                List {
                    ForEach(items, id: \.self) { item in
                        CaseItemView()
                            .shadow(color: .black.opacity(0.4), radius: 6)
                            .listRowSeparator(.hidden)
                    }
                }
                .listStyle(.plain)
            }
        }
        .paddingTop(40)
        .overlay (alignment: .bottom) {
            Button(action: {
                router.to(name: .phoneLogin)
            }, label: {
                Image(systemName: "plus.viewfinder")
                    .resizable()
                    .frame(width: 30, height: 30)
                    .foregroundStyle(.white)
                    .padding(10)
                    .background(Theme.color.primaryBackgroundColor)
                    .clipShape(Circle())
                    .shadow(color: .black.opacity(0.2),  radius: 10)
            })
            .offset(x: 0, y: -20)
        }
        .onAppear(perform: {
            let allRecord = CaseDBManager.shared.queryAllRecords()
            self.items = allRecord
        })
    }
}
